/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.WindowManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

public class CustomTools {

    /**
     * 设置状态栏透明.
     *
     * @param activity activity
     */
    public static void setStatusBarTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //5.0及以上
            View decorView = activity.getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams
                    .FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    /**
     * url追加参数
     *
     * @param srcUrl     url
     * @param parameters 参数
     * @return 新的url.
     */
    public static String appendParameters2Url(String srcUrl, Map<String, String> parameters) {
        String url = CustomTools.correctUrlFormat(srcUrl);
        if (null == url || url.isEmpty() || parameters == null || parameters.isEmpty()) {
            return url;
        }
        Set<String> strings = parameters.keySet();
        String value;
        for (String next : strings) {
            value = parameters.get(next);
            if (value == null || value.length() == 0) {
                continue;
            }
            if (url.contains("?" + next + "=") || url.contains("&" + next + "=")) {
                continue;
            }

            if (url.contains("?")) {
                url = url + "&" + next + "=" + value;
                continue;
            }

            if (-1 == url.indexOf('/', "https://".length())) {
                url = url + "/?" + next + "=" + value;
                continue;
            }

            if (url.indexOf('/', "https://".length()) == url.length() - 1) {
                url = url + "?" + next + "=" + value;
                continue;
            }

            if (url.endsWith("/")) {
                url = url.substring(0, url.length() - 1) + "?" + next + "=" + value;
                continue;
            }
            url = url + "?" + next + "=" + value;
        }
        return url;
    }

    /**
     * 纠正Url多余的空格.
     *
     * @param srcUrl 待纠正的Url
     * @return 纠正后的Url
     */
    public static String correctUrlFormat(String srcUrl) {

        if (null == srcUrl || srcUrl.isEmpty()) {
            return srcUrl;
        }

        if (0 == srcUrl.indexOf(':')) {
            return "http" + srcUrl.trim();
        }

        if (-1 == srcUrl.indexOf(':')) {
            return "http://" + srcUrl.trim();
        }

        return srcUrl.trim();
    }

}
